package com.drinks.yiniang.ui.widget.nav;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.tab.TabbarEntity;
import com.hansen.library.ui.widget.textview.DragPointView;
import com.hansen.library.utils.ColorUtils;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.ScreenSizeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义底部tabBar
 * Created by han on 2018/10/14 0014.
 */

public class TabBarLayout extends LinearLayout {

    private Context mContext;

    private int mTextSize = 12;
    private int mTextColor = Color.parseColor("#282829");
    private int mTextSelectColor = Color.parseColor("#ef3024");
    private int mImageWidth = 26;
    private int mImageHeight = 26;
    private int checkTabbarPos = 0;

    private boolean hasTabBarTopLine;

    private Paint mPaint;
    private Path mPath;

    private List<TabbarEntity> mTabbarList;

    private OnTabbarClickListener mOnTabbarClickListener;

    public TabBarLayout(Context context) {
        this(context, null);
    }

    public TabBarLayout(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabBarLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        mContext = context;

        setOrientation(HORIZONTAL);

        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.TabBarLayoutStyleable);

        mTextSize = typedArray.getDimensionPixelSize(R.styleable.TabBarLayoutStyleable_textSize, mTextSize);
        mTextColor = typedArray.getColor(R.styleable.TabBarLayoutStyleable_textColor, mTextColor);
        mTextSelectColor = typedArray.getColor(R.styleable.TabBarLayoutStyleable_textSelectColor, mTextSelectColor);
        mImageWidth = typedArray.getInteger(R.styleable.TabBarLayoutStyleable_imageWidth, mImageWidth);
        mImageHeight = typedArray.getInteger(R.styleable.TabBarLayoutStyleable_imageHeight, mImageHeight);
        checkTabbarPos = typedArray.getInteger(R.styleable.TabBarLayoutStyleable_checkTabbar, checkTabbarPos);

        hasTabBarTopLine = typedArray.getBoolean(R.styleable.TabBarLayoutStyleable_tabBarTopLine, false);

        typedArray.recycle();

        if(hasTabBarTopLine) {
            mPaint = new Paint();
            mPath = new Path();

            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(ColorUtils.getColorById(mContext, R.color.color_d8d8d8));
            mPaint.setStrokeWidth(2);
            mPaint.setAntiAlias(true);

            //false 才会执行onDraw()
            setWillNotDraw(false);
        }

        initTest();
    }

    private void initTest() {
        if (isInEditMode()) {
            mTabbarList = new ArrayList<>(5);

            mTabbarList.add(new TabbarEntity(R.string.text_main_home, R.mipmap.icon_tab_home_n, R.mipmap.icon_tab_home_p));
            mTabbarList.add(new TabbarEntity(R.string.text_category, R.mipmap.icon_tab_category_n, R.mipmap.icon_tab_category_p));
            mTabbarList.add(new TabbarEntity(R.string.text_community, R.mipmap.icon_tab_community_n, R.mipmap.icon_tab_community_p));
            mTabbarList.add(new TabbarEntity(R.string.text_shopping_cart, R.mipmap.icon_tab_cart_n, R.mipmap.icon_tab_cart_p));
            mTabbarList.add(new TabbarEntity(R.string.text_my, R.mipmap.icon_tab_me_n, R.mipmap.icon_tab_me_p));

            setTabbarData(mTabbarList);
        }
    }

    public void setTabbarData(List<TabbarEntity> mList) {
        if (CommonUtils.isEmptyList(mList)) {
            return;
        }

        mTabbarList = mList;

        LayoutParams paramsIv = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        LayoutParams paramsTv = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        AppCompatImageView iv;
        AppCompatTextView tv;

        for (int i = 0; i < mTabbarList.size(); i++) {
            FrameLayout frameLayout = new FrameLayout(mContext);

            LinearLayout layout = new LinearLayout(mContext);

            frameLayout.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT, 1f));

            layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1f));

            layout.setOrientation(VERTICAL);
            layout.setGravity(Gravity.CENTER);

            paramsIv.width = ScreenSizeUtils.dp2px(mContext, mImageWidth);
            paramsIv.height = ScreenSizeUtils.dp2px(mContext, mImageHeight);

            iv = new AppCompatImageView(mContext);
            tv = new AppCompatTextView(mContext);

            if(checkTabbarPos == i) { //选中的tabbar
                iv.setImageResource(mTabbarList.get(i).getImageSelectSrcResId());
                tv.setTextColor(mTextSelectColor);
            } else {
                iv.setImageResource(mTabbarList.get(i).getImageSrcResId());
                tv.setTextColor(mTextColor);
            }

            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, mTextSize);
            tv.setText(mTabbarList.get(i).getTextResId());
            tv.setGravity(Gravity.CENTER_HORIZONTAL);

            mTabbarList.get(i).setIv(iv);
            mTabbarList.get(i).setTv(tv);

            final int tempPos = i;

            layout.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeTabbar(v, tempPos);
                }
            });

            layout.addView(iv, paramsIv);
            layout.addView(tv, paramsTv);

            frameLayout.addView(layout);

            if(mTabbarList.get(i).isHasRedDot()) {
                LayoutInflater.from(mContext).inflate(R.layout.item_red_dot, frameLayout, true);
            }

            addView(frameLayout);
        }
    }

    private void changeTabbar(View v, int currentPos) {
        if(mOnTabbarClickListener != null) {
            if(mOnTabbarClickListener.onTabbarClickListener(v, currentPos)) {
                return;
            }
        }

        if(CommonUtils.isArrayIndexOutOfBounds(mTabbarList, currentPos) || CommonUtils.isArrayIndexOutOfBounds(mTabbarList, checkTabbarPos)) {
            return;
        }

        if(currentPos != checkTabbarPos) {
            TabbarEntity mCurrentTabbar = mTabbarList.get(checkTabbarPos);
            TabbarEntity mSelectTabbar = mTabbarList.get(currentPos);

            mCurrentTabbar.getIv().setImageResource(mCurrentTabbar.getImageSrcResId());
            mCurrentTabbar.getTv().setTextColor(mTextColor);

            mSelectTabbar.getIv().setImageResource(mSelectTabbar.getImageSelectSrcResId());
            mSelectTabbar.getTv().setTextColor(mTextSelectColor);

            checkTabbarPos = currentPos;
        }
    }

    public void changeTabBar(int currentPos) {
        if(currentPos == checkTabbarPos) {
            return;
        }

        if(CommonUtils.isArrayIndexOutOfBounds(mTabbarList, currentPos) || CommonUtils.isArrayIndexOutOfBounds(mTabbarList, checkTabbarPos)) {
            return;
        }

        TabbarEntity mCurrentTabbar = mTabbarList.get(checkTabbarPos);
        TabbarEntity mSelectTabbar = mTabbarList.get(currentPos);

        mCurrentTabbar.getIv().setImageResource(mCurrentTabbar.getImageSrcResId());
        mCurrentTabbar.getTv().setTextColor(mTextColor);

        mSelectTabbar.getIv().setImageResource(mSelectTabbar.getImageSelectSrcResId());
        mSelectTabbar.getTv().setTextColor(mTextSelectColor);

        checkTabbarPos = currentPos;
    }

    public void setTabRedDot(int pos, int unread) {
        if(CommonUtils.isArrayIndexOutOfBounds(mTabbarList, pos)) {
            return;
        }

        if(!mTabbarList.get(pos).isHasRedDot()) {
            return;
        }

        FrameLayout tabView = (FrameLayout) this.getChildAt(pos);

        if(tabView == null) {
            return;
        }

        DragPointView unreadTV = tabView.findViewById(R.id.tv_drag_new_msg_label);

        if(unreadTV != null) {
            if(unread > 0) {
                unreadTV.setVisibility(VISIBLE);
                unreadTV.setText(CommonUtils.unreadMessageShowRule(unread));
            } else {
                unreadTV.setVisibility(GONE);
            }
        }
    }

    public void setOnTabbarClickListener(OnTabbarClickListener mOnTabbarClickListener) {
        this.mOnTabbarClickListener = mOnTabbarClickListener;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(hasTabBarTopLine) {
            mPath.reset();
            mPath.moveTo(0, 0);
            mPath.lineTo(this.getWidth(), 0);
            canvas.drawPath(mPath, mPaint);
        }
    }

    public interface OnTabbarClickListener {
        //return true, 不执行change tabbar
        boolean onTabbarClickListener(View v, int pos);
    }
}
